package com.situ.stmall.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stmall.manager.entity.Goods;
import com.situ.stmall.manager.entity.GoodsPic;
import com.situ.stmall.manager.mapper.GoodsMapper;
import com.situ.stmall.manager.service.GoodsPicService;
import com.situ.stmall.manager.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    //注入goodsPic的service的对象
    @Resource
    private GoodsPicService goodsPicService;
    @Autowired
    private GoodsMapper goodsMapper;
    /**
     * 添加
     *
     * @param goods
     */
    @Override

    /**
     * 使用Spring事务管理
     */
    @Transactional(
            isolation = Isolation.SERIALIZABLE, //ACID   隔离级别
            propagation = Propagation.REQUIRED, //传播机制 七个 面试题
            rollbackFor = Exception.class // 回滚， 当方法抛出任何异常，回滚事务
    )
    public int add(Goods goods) throws Exception {
        //参数验证
        if(goods.getName() == null || goods.getName().length() == 0){
            throw new Exception("商品名不能为空！");
        }
        if(goods.getDscp() == null || goods.getDscp().length() == 0){
            throw new Exception("商品简介不能为空！");
        }
        if(goods.getPrice() == null){
            throw  new Exception("售价不能为空！");
        }
        if(goods.getMarkPrice() == null){
            throw  new Exception("标价不能为空！");
        }
        if(goods.getColor() == null || goods.getColor().length() == 0){
            throw new Exception("颜色不能为空！");
        }
        if(goods.getVersion() == null || goods.getVersion().length() == 0){
            throw new Exception("版本不能为空！");
        }
        if(goods.getCount() == null){
            throw  new Exception("数量不能为空！");
        }
        if(goods.getRecom() == null){
            throw  new Exception("推荐不能为空！");
        }
        if(goods.getCategoryId() == null){
            throw  new Exception("分类不能为空！");
        }

        //只保存了商品信息，没有保存图片
        //先保存商品
        int code = goodsMapper.insert(goods);
        if(code > 0){
            //再保存图片,
            for (GoodsPic pic: goods.getPics()) {
                pic.setGoodsId(goods.getId());
            }
            goodsPicService.addPatch(goods.getPics());
        }
        return code;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public int remove(Integer id) throws Exception {
        if(goodsMapper.selectById(id) == null){
            throw new Exception("ID不存在");
        }
        return goodsMapper.delete(id);
    }

    /**
     * 更新
     *
     * @param goods
     */
    @Override
    public int edit(Goods goods) throws Exception {
        if(goodsMapper.selectById(goods.getId()) == null){
            throw new Exception("ID不存在");
        }
        int code = goodsMapper.update(goods);
        goodsPicService.removeByGoodsId(goods.getId());
        if(goods.getPics() != null && goods.getPics().size() >0){
            for (GoodsPic pic: goods.getPics()) {
                pic.setGoodsId(goods.getId());
            }
            goodsPicService.addPatch(goods.getPics());
        }
        return goodsPicService.addPatch(goods.getPics()
        );
    }

    /**
     * 根据id查询
     *
     * @param id
     */
    @Override
    public Goods getById(Integer id) {
        return goodsMapper.selectById(id);
    }

    /**
     * 查询
     *
     * @param goods
     */
    @Override
    public List<Goods> search(Goods goods) {
        return goodsMapper.select(goods);
    }

    /**
     * 分页
     *
     * @param page
     * @param limit
     * @param goods
     * @return
     */
    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Goods goods) {
        //1.启动分页
        PageHelper.startPage(page,limit);
        //2.调用Mapper层
        List<Goods> list =  goodsMapper.select(goods);
        //3.构造pageInfo对象
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
